package cn.ichiva.luckysheet.utils;

import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author pzq
 * @version 1.0
 * @date 2022/1/4 11:28
 */
public class Sort {
   static String  datajson="{\n" +
           "\t\"data\": [{\n" +
           "\t\t\"amount\": \"100\",\n" +
           "\t\t\"month\": \"1\",\n" +
           "\t\t\"areaname\": \"华北\",\n" +
           "\t\t\"year\": \"2020\",\n" +
           "\t\t\"price\": \"5\",\n" +
           "\t\t\"id\": \"1\",\n" +
           "\t\t\"dept\": \"河北\",\n" +
           "\t\t\"settleamount\": \"100\"\n" +
           "\t}, {\n" +
           "\t\t\"amount\": \"202\",\n" +
           "\t\t\"month\": \"2\",\n" +
           "\t\t\"areaname\": \"华南\",\n" +
           "\t\t\"year\": \"2021\",\n" +
           "\t\t\"price\": \"6\",\n" +
           "\t\t\"id\": \"12\",\n" +
           "\t\t\"dept\": \"河南\",\n" +
           "\t\t\"settleamount\": \"202\"\n" +
           "\t}, {\n" +
           "\t\t\"amount\": \"205\",\n" +
           "\t\t\"month\": \"1\",\n" +
           "\t\t\"areaname\": \"华南\",\n" +
           "\t\t\"year\": \"2020\",\n" +
           "\t\t\"price\": \"5\",\n" +
           "\t\t\"id\": \"15\",\n" +
           "\t\t\"dept\": \"南京\",\n" +
           "\t\t\"settleamount\": \"205\"\n" +
           "\t}, {\n" +
           "\t\t\"amount\": \"201\",\n" +
           "\t\t\"month\": \"2\",\n" +
           "\t\t\"areaname\": \"华北\",\n" +
           "\t\t\"year\": \"2021\",\n" +
           "\t\t\"price\": \"5\",\n" +
           "\t\t\"id\": \"20\",\n" +
           "\t\t\"dept\": \"长沙\",\n" +
           "\t\t\"settleamount\": \"201\"\n" +
           "\t}, {\n" +
           "\t\t\"amount\": \"200\",\n" +
           "\t\t\"month\": \"1\",\n" +
           "\t\t\"areaname\": \"华中\",\n" +
           "\t\t\"year\": \"2020\",\n" +
           "\t\t\"price\": \"5\",\n" +
           "\t\t\"id\": \"21\",\n" +
           "\t\t\"dept\": \"武汉\",\n" +
           "\t\t\"settleamount\": \"200\"\n" +
           "\t}, {\n" +
           "\t\t\"amount\": \"202\",\n" +
           "\t\t\"month\": \"2\",\n" +
           "\t\t\"areaname\": \"华南\",\n" +
           "\t\t\"year\": \"2021\",\n" +
           "\t\t\"price\": \"6\",\n" +
           "\t\t\"id\": \"22\",\n" +
           "\t\t\"dept\": \"南京\",\n" +
           "\t\t\"settleamount\": \"202\"\n" +
           "\t}, {\n" +
           "\t\t\"amount\": \"202\",\n" +
           "\t\t\"month\": \"2\",\n" +
           "\t\t\"areaname\": \"华南\",\n" +
           "\t\t\"year\": \"202156\",\n" +
           "\t\t\"price\": \"9\",\n" +
           "\t\t\"id\": \"12\",\n" +
           "\t\t\"dept\": \"河南\",\n" +
           "\t\t\"settleamount\": \"202\"\n" +
           "\t}]\n" +
           "}";
    public static void main(String[] args) {
//        sortkk();
        sortByNKeys();
        Map<String,Object> map= new HashMap<>();

    }
    public static List<Map<String,Object>> sortkk(){
        String[] str = new String[]{"areaname","amount","4"};
        JSONObject jsonObjectStr  = JSONObject.parseObject(datajson);
        List<Map<String,Object>> maps = (List<Map<String, Object>>) jsonObjectStr.get("data");
        maps=maps.stream().sorted((map1,map2)->{
            if (map1.get("areaname").equals(map2.get("areaname").toString())) {
                return map2.get("areaname").toString().compareTo(map1.get("areaname").toString());
            }
//            if(map2.get("dept").toString().equals(map1.get("dept").toString())) {
//                return map2.get("dept").toString().compareTo(map1.get("dept").toString());
//            }
            else{
                return map2.get("dept").toString().compareTo(map1.get("dept").toString());
            }
//            // 如果相同则进行第二次比较
//            if (map2.get("areaname").toString().compareTo(map1.get("areaname").toString()) == 0) {
//                if (map2.get("dept").toString().compareTo(map1.get("dept").toString()) == 0) {
//// 第三次比较
//                    if (map2.get("amount").toString().compareTo(map1.get("amount").toString()) == 0) {
//// 第四次比较
//                        return map2.get("amount").toString().compareTo(map1.get("amount").toString());
//                    }
//                    return map2.get("dept").toString().compareTo(map1.get("dept").toString());
//                }
//                return map2.get("areaname").toString().compareTo(map1.get("areaname").toString());
//            }
//            return map2.get("areaname").toString().compareTo(map1.get("areaname").toString());
        }).collect(Collectors.toList());
        return maps;
    }
    public static List<Map<String, Object>> sortByNKeys() {
        String sql = "select author,price,publisher,isbn,title from books";
//        List<Map<String, Object>> bookList = jdbcTp.queryForList(sql);
        JSONObject jsonObjectStr  = JSONObject.parseObject(datajson);
        List<Map<String,Object>> maps = (List<Map<String, Object>>) jsonObjectStr.get("data");
        List<Map<String, Object>> bookList =maps;

        // 使用java 8的 stream 功能进行排序
        // order by author,price desc,publisher
        /**
         * 升序返回-1，倒序返回1即可 有多个key需要比较的时候(不考虑null或者""的情况),当第n个key返回的是0的时候继续比较，如果不是
         * 则根据第n个key的比较值返回需要的结果。 譬如第1个key的比较结果是0,则比骄傲第2个， 假定第二的key的比较结果是-1
         * 如果要根据第二个倒序，则返回1；如果要根据第二个升序，则直接发那会-1即可 以此类推
         */

        Collections.sort(bookList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                // 进行判断
                int cp1 = ((String) o1.get("areaname")).compareTo((String) o2
                        .get("areaname"));
//                BigDecimal price1 = new BigDecimal(o1.get("price").toString());
//                BigDecimal price2 = new BigDecimal(o1.get("price").toString());
                String dept1 = (o1.get("dept").toString());
                String dept2 =(o1.get("dept").toString());
                int cp2 = dept1.compareTo(dept2);

                String amount1 = o1.get("amount").toString();
                String amount2 = o2.get("amount").toString();
                int cp3 = amount1.compareTo(amount2);

                if (cp1 == 0) {
                    if (cp2 == 0) {
                        if (cp3 == 0) {
                            return 0;
                        } else {
                            return cp3;
                        }
                    } else {
                        return cp2 * -1;
                    }
                } else {
                    return cp1;
                }
                // return ;
            }
        });

        return bookList;
    }
}
